#!/usr/bin/env python

from WSOption import WSOption
import sys

class WSOptionGeneratorDict(WSOption):
	_allowed_class = ['MaskGenerator', 'DictFromFile']
	dict_generator = None

	def __init__(self, name, description, value, required, flags, advanced_option_name, module=None):
		self.advanced_option_name = advanced_option_name
		super().__init__( name, description  + ' (allowed ' + ', '.join(self._allowed_class) +')',
			value, required, flags, module)

	def __my_import(self, name):
		sys.path.append('classes/dictgen/')
		mod = __import__(name)
		the_class = getattr(mod, name)
		return the_class
	def loadDictGen(self, dictgen_name):
		module = self.__my_import(dictgen_name)
		return module()

	@property
	def value(self):
		return self.dict_generator.name if self.dict_generator else ''
	@value.setter
	def value(self, new_value):
		if new_value in self._allowed_class:
			self.dict_generator = self.loadDictGen(new_value)
			if self.module and self.dict_generator:
				self.module.options[self.advanced_option_name] = self.dict_generator.options